package org.example.lotterysystem.controller;

import cn.hutool.log.Log;
import jakarta.validation.Valid;
import org.example.lotterysystem.common.errorcode.ControllerErrorCodeConstants;
import org.example.lotterysystem.common.exception.ControllerException;
import org.example.lotterysystem.common.pojo.CommonResult;
import org.example.lotterysystem.common.utils.JacksonUtil;
import org.example.lotterysystem.controller.param.CreatePrizeParam;
import org.example.lotterysystem.controller.param.PageParam;
import org.example.lotterysystem.controller.result.FindPrizeListResult;
import org.example.lotterysystem.service.PictureService;
import org.example.lotterysystem.service.PrizedService;
import org.example.lotterysystem.service.dto.PageListDTO;
import org.example.lotterysystem.service.dto.PrizeDTO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import java.util.stream.Collectors;

@RestController
public class PrizeController {
    private static final Logger logger= LoggerFactory.getLogger(PrizeController.class);
    @Autowired
    private PictureService pictureService;
    @Autowired
    private PrizedService prizedService;

    /**
     * 上传图片
     * @param file
     * @return
     */
    @RequestMapping("/pic/upload")
    public String uploadPic(MultipartFile file){
        return pictureService.savePicture(file);
    }

    /**
     * 创建奖品
     * RequestPart用于接收表单数据
     * @param param
     * @param picFile
     * @return
     */
    @RequestMapping("/prize/create")
    public CommonResult<Long> createPrize(@Valid @RequestPart("param") CreatePrizeParam param,
                                          @RequestPart("prizePic") MultipartFile picFile){
        logger.info("findPrizedList PageParam:{}", JacksonUtil.writeValueAsString(param));
        return CommonResult.success(prizedService.createPrize(param,picFile));
    }
    @RequestMapping("/prize/findPrizeList")
    public CommonResult<FindPrizeListResult>findPrizeList(PageParam param){
        logger.info("findPrizeList PageParam:{}",JacksonUtil.writeValueAsString(param));
        PageListDTO<PrizeDTO> pageListDTO = prizedService.findPrizeList(param);
        return CommonResult.success(convertToFindPrizeListResult(pageListDTO));


    }

    private FindPrizeListResult convertToFindPrizeListResult(PageListDTO<PrizeDTO> pageListDTO) {
        if(pageListDTO==null){
            throw new ControllerException(ControllerErrorCodeConstants.FIND_PRIZE_ERROR);
        }
        FindPrizeListResult findPrizeListResult=new FindPrizeListResult();
        findPrizeListResult.setTotal(pageListDTO.getTotal());
        findPrizeListResult.setRecords(
                pageListDTO.getRecords().stream().map(prizeDTO -> {
                  FindPrizeListResult.PrizeInfo prizeInfo=new FindPrizeListResult.PrizeInfo();
                  prizeInfo.setPrizeId(prizeInfo.getPrizeId());
                  prizeInfo.setPrizeName(prizeInfo.getPrizeName());
                  prizeInfo.setDescription(prizeDTO.getDescription());
                  prizeInfo.setPrice(prizeDTO.getPrice());
                  prizeInfo.setImageUrl(prizeDTO.getImageUrl());
                  return prizeInfo;
                }).collect(Collectors.toList())
        );
        return findPrizeListResult;
    }
}
